package io;

import java.io.File;

import javax.swing.JFileChooser;

import view.Designer;

/**
 * A class to model the save dialog box for files
 * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer
 *
 */
public class FileSaveDialog
{	
	private static JFileChooser chooser;
	private static FileSaveDialog instance;
	private SaveFileFilter filter;
	
	private FileSaveDialog(File baseFolder)
	{
		if (!baseFolder.isDirectory())
		{
			baseFolder = new File(".");
		}
		
		chooser = new JFileChooser(baseFolder);
		chooser.addChoosableFileFilter(new SaveFileFilter());
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		filter = new SaveFileFilter();	
	}
	
	public static FileSaveDialog getInstance(){
		if(instance == null){
			instance = new FileSaveDialog(new File("C:\\"));
			return instance;
		}
		return instance;
	}
	
	/**
	 * Opens the dialog window
	 * @return the first selected file in the dialog window
	 */
	public File show()
	{
		int returnVal = chooser.showSaveDialog(Designer.getInstance());
		if(returnVal == JFileChooser.APPROVE_OPTION) {
		
			if(filter.accept(chooser.getSelectedFile())){
				return chooser.getSelectedFile();
			}else{
				Designer.getInstance().showMessageDialog("Incorrect file extension. Please export again with a correct file extension.");
				return null;
			}
		
		}
		return null;
	}
	
	public static JFileChooser getDialog(){
		return chooser;
	}
	
}
